package com.anyext.storage.asm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.anyext.storage.jdbc.RowCallback;
import com.anyext.storage.model.Address;
import com.anyext.storage.model.DeptInfo;
import com.anyext.storage.model.UserInfo;

public class UserInfoRowCallback extends RowCallback<UserInfo> {

	private Map<String, UserInfo> businessMap = new HashMap<String, UserInfo>();

	@Override
	public void processRow(ResultSet rs) throws SQLException {

		StringBuffer userInfoKey = new StringBuffer();
		String platUsername = rs.getString("platUsername");
		userInfoKey.append(platUsername);
		Date registerTime = rs.getTimestamp("registerTime");
		userInfoKey.append(registerTime);
		Integer deptNumber = rs.getInt("deptNumber");
		String deptCode = rs.getString("deptCode");
		// ……

		UserInfo userInfo = businessMap.get(userInfoKey.toString());
		if (userInfo == null) {
			userInfo = new UserInfo();
			userInfo.setPlatUsername(platUsername);
			userInfo.setRegisterTime(registerTime);
			// ……

			DeptInfo deptInfo1 = new DeptInfo();
			deptInfo1.setDeptCode(deptCode);
			deptInfo1.setDeptNumber(deptNumber);
			// ……
			userInfo.setDeptInfo(deptInfo1);

			DeptInfo deptInfo2 = new DeptInfo();
			deptInfo2.setDeptCode(deptCode);
			deptInfo2.setDeptNumber(deptNumber);
			// ……
			userInfo.setDeptInfo(deptInfo2);

			businessMap.put(userInfoKey.toString(), userInfo);
			results.add(userInfo);
		}
		
		StringBuffer addressesKey1 = new StringBuffer(userInfoKey);
		String address1 = rs.getString("address1");
		addressesKey1.append(address1);
		String address2 = rs.getString("address2");
		if (!businessMap.containsKey(addressesKey1.toString())) {
			List<Address> addresses = userInfo.getAddress();
			if (addresses == null) {
				addresses = new ArrayList<Address>();
				userInfo.setAddress(addresses);
			}
			Address address = new Address();
			address.setAddress1(address1);
			address.setAddress2(address2);
			addresses.add(address);
			businessMap.put(addressesKey1.toString(), null);
		}

		StringBuffer addressesKey2 = new StringBuffer(userInfoKey);
		String address3 = rs.getString("address3");
		addressesKey2.append(address3);
		String address4 = rs.getString("address4");
		if (!businessMap.containsKey(addressesKey2.toString())) {
			List<Address> addresses = userInfo.getAddress();
			if (addresses == null) {
				addresses = new ArrayList<Address>();
				userInfo.setAddress(addresses);
			}
			Address address = new Address();
			address.setAddress1(address3);
			address.setAddress2(address4);
			addresses.add(address);
			businessMap.put(addressesKey2.toString(), null);
		}
		// ……
	}

}
